﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DTO;
using DAO;

namespace GUI
{
    public partial class Main : Form
    {
        enum State { AddNew, Update, Delete,None };
        State AppState = State.None;
        void SetAppState()
        {
            SaveButton.Enabled = false;
            DeleteButton.Enabled = true;
            AddNewButton.Enabled = true;
            EditButton.Enabled = true;
            hocsinhDTODataGridView.Enabled = true;
            SetControls(false);
            if (AppState == State.AddNew)
            {
                DeleteButton.Enabled = false;
                EditButton.Enabled = false;
                AddNewButton.Enabled = false;
                SaveButton.Enabled = true;
                SetControls(true, true);
            }
            if (AppState == State.Update)
            {
                DeleteButton.Enabled = false;
                EditButton.Enabled = false;
                AddNewButton.Enabled = false;
                SaveButton.Enabled = true;
                hocsinhDTODataGridView.Enabled = false;
                SetControls(true);
            }
        }
        private void SetControls(bool value,bool id=false)
        {
            mahsTextBox.Enabled = id;
            holotTextBox.Enabled = value;
            tenTextBox.Enabled = value;
            ngaysinhDateTimePicker.Enabled = value;
            malopComboBox.Enabled = value;
        }
        public Main()
        {
            InitializeComponent();
        }
        private void Main_Load(object sender, EventArgs e)
        {
            LoadData();
        }

        private void LoadData()
        {
            hocsinhDTOBindingSource.DataSource = HocsinhDAO.SelectAll();
            SetAppState();
        }
        HocsinhDTO hsBuf=null;
        private void AddNewButton_Click(object sender, EventArgs e)
        {
            AppState = State.AddNew;
            hsBuf = hocsinhDTOBindingSource.AddNew() as HocsinhDTO;
            SetAppState();
        }
        
        private void SaveButton_Click(object sender, EventArgs e)
        {
            if (AppState == State.AddNew)
            {
                HocsinhDAO.AddNew(hsBuf);
                hsBuf = null;
            }
            if (AppState == State.Update)
            {
                HocsinhDAO.Update(hsBuf);
                hsBuf = null;
            }
            AppState = State.None;
            SetAppState();
        }

        private void DeleteButton_Click(object sender, EventArgs e)
        {
            hsBuf = hocsinhDTOBindingSource.Current as HocsinhDTO;
            hocsinhDTOBindingSource.RemoveCurrent();
            HocsinhDAO.Delete(hsBuf);
            hsBuf = null;
        }

        private void EditButton_Click(object sender, EventArgs e)
        {
            AppState = State.Update;
            hsBuf = hocsinhDTOBindingSource.Current as HocsinhDTO;
            SetAppState();
        }
        private void HuyButton_Click(object sender, EventArgs e)
        {
            AppState = State.None;
            LoadData();
        }
    }
}
